using System;
using System.Net.Sockets;
using System.Text;

namespace Framework.Network
{
    public static class NetUtil
    {
        public const int LobbyConnect = 1;
        public const int ChatConnect = 2;
        
        public static byte[] GetErrorMessage(Exception e)
        {
            SocketException se = (SocketException)e;
            if(se != null)
            {
                var s = $"{se.SocketErrorCode}:{se.Message}.(NativeErrorCode: {se.NativeErrorCode})";
                return Encoding.Default.GetBytes(s);
            }
            
            return Encoding.Default.GetBytes(e.Message);
        }
        
        public static short ToInt16(byte[] bytes)
        {
            int v1 = bytes[0];
            int v2 = bytes[1];

            var value = v1
                        | (v2 << 8);
            return (short)value;
        }
    }
}